package com.fcat.gateway.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 返回信息模型类
 */
public class Result<T> {

    public static final int FAILURE_CODE = -1;
    public static final int SUCCESS_CODE = 0;
    public static final String SUCCESS_MESSAGE = "操作成功";
    public static final String FAILURE_MESSAGE = "操作失败";

    private Integer code;

    private String msg;

    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("code:").append(getCode());
        buffer.append(",msg:").append(getMsg());
        buffer.append(",data:").append(getData());
        return buffer.toString();
    }

    public static <T> Result<T> success(T t) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setMsg(SUCCESS_MESSAGE);
        result.setData(t);
        return result;
    }

    public static <T> Result<Map<String, Object>> pageList(int count, List<T> list, Object params) {
        Result<Map<String, Object>> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setMsg(SUCCESS_MESSAGE);

        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("rows", list);
        dataMap.put("total", count);
        dataMap.put("params", params);
        result.setData(dataMap);
        return result;
    }

    public static <T> Result<T> failure() {
        Result<T> result = new Result<>();
        result.setCode(FAILURE_CODE);
        result.setMsg(FAILURE_MESSAGE);
        return result;
    }

    public static <T> Result<T> failure(StateCode stateCode) {
        return failure(stateCode, stateCode.msg);
    }

    public static <T> Result<T> failure(StateCode stateCode, T data) {
        Result<T> result = new Result<>();
        result.setCode(stateCode.getCode());
        result.setMsg(stateCode.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failure(StateCode stateCode, String message) {
        Result<T> result = new Result<>();
        result.setCode(stateCode.getCode());
        result.setMsg(message);
        return result;
    }
	
	public boolean successOrNot(){
        if(SUCCESS_CODE == this.getCode()){
            return true;
        }
        return false;
    }

}
